/**
 * 登录模块，提供登录相关业务功能
 * 
 * @Date: 2015-6-16
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */
define(["jquery", "datagridServ","sysmanMain", "bizProto","btnServ","msgServ","flowServ","uploadfiles","panelUtil","buttonEvent"],
		function($,datagridServ, sysmanMain, bizProto,btnServ,msgServ,flowServ,uploadfiles,panelUtil,buttonEvent) {

			'use strict';

			var _bizProto,_moduleConfig,_buttonConfig,_defaultConfig,
				//锚记datagrid目标元素
				$dg,
				//初始化后的datagrid dom对象
				$grid,
				//编辑弹窗对象及弹窗按钮对象
				$edit_window,$edit_window_height=700,$edit_window_width=700,window_btn,
				
				//流程执行信息grid
				_historicGrid,
				//业务表单对象
				$form,
				_defaultConfig,
				
				
				//明细列表dom对象clas属性定义
				gridSelector='table.dg-detail'
				
				
				
			var public_method = {				
				
				init:function(_options){
					_defaultConfig ={
						doc:null,
						rootId:null,				
						panelSelector:'div.well',
						subSystemCode:null,
						moduleCode:null,
						editWindow:null,
						editbtnSelector:'div.edit-btn',
						initQueryParams:null,
						initElement:null,
						historicUrl:null,
						historicField:null,
						auditEnable:false,//启用审核
						editInitialized:false,
						beforeSubmit:null,//保存前验证函数
						afterSubmit:null//保存后执行函数
					}
					_moduleConfig = sysmanMain.getCurrentModuConfig();
					if (window.console) {
						console.info( _moduleConfig.fullBizcode + ' modules gets ready and been loaded.');
					}
					
					
					_defaultConfig = $.extend({},_moduleConfig,_defaultConfig,_options)			
					_bizProto = new bizProto(_defaultConfig);			
					_bizProto.gridHeight -= 15
					
					_defaultConfig.bizProto=_bizProto
					
					
					
					
					if(_defaultConfig.editWindow){
						$edit_window = $('#'+_defaultConfig.editWindow)						
					}
					
					
					_buttonConfig={									    
						 "保存":private_method.submit,
						 "取消":private_method.cancel	,
						 "上传":private_method.upload,
						 "附件":private_method.uploadInit,
						 "删除":private_method.delupload,
						 "返回":private_method.changePanel,
						 "提交审核":private_method.auditSubmit,
						 "审核通过":private_method.auditPermit,
						 "审核未通过":private_method.auditDenied
					}
					
					//增加扩展按钮事件响应
					if(_defaultConfig.buttonExtends){
						_buttonConfig = $.extend(_buttonConfig,_defaultConfig.buttonExtends())
					}
					
					_defaultConfig.buttonConfig = _buttonConfig
					_defaultConfig.editInitialized = true;
					//保存模块初始化信息
					sysmanMain.setModuConf(_moduleConfig.fullBizcode,_defaultConfig)
					
					private_method.init();				
				}
				
			}

			var private_method = {
				
				
				/**
				 * 初始化上传附件dom对象
				 */
				uploadInit:function(){
					
					if(private_method.checkAuditStatus()){
						msgServ.Msgslide('已审核记录无法更改附件')
						return
					}
					
					var isSaved = false
					
					private_method.changePanel();
					
					//查找附件dom对象定义
					var _uploadList = $('#' + _moduleConfig.editWindow + ' div.upload-panel div.table-archive')
					
					//判断对象是否存在
					if(_uploadList.length>0){
						var selectedRecord = private_method.getCurrentStatus();
						var _data = {bizcode:_moduleConfig.moduleCode}
						if(selectedRecord && selectedRecord.id){
							_data.bizno=selectedRecord.id
						}
						var uploadParam = {
							data:_data,
							rootPath:_bizProto.rootPath,
							biztableid:_moduleConfig.editWindow + ' div.upload-panel div.table-archive',
							toolbarid:_moduleConfig.editWindow +' div.upload-panel div.archive-btn a.file_upload_btn'
						}
						uploadfiles.initList(uploadParam)
						
						if(_moduleConfig.moduleCode){
							var _initParam = {
								data:_data,								
								queue_id:_moduleConfig.editWindow + ' div.upload-panel form.upload-form #queue',
								file_id:_moduleConfig.editWindow + ' div.upload-panel form.upload-form #file_upload',
								rootPath:_moduleConfig.bizProto.rootPath,
								rootid:_moduleConfig.editWindow}
							
							if(_moduleConfig.fileType){
								_initParam.fileType = _moduleConfig.fileType
							}
							uploadfiles.init(_initParam)
						}
					}
				},
				
				//执行附件上传
				upload:function(){
//					_moduleConfig = sysmanMain.getCurrentModuConfig();
//					var _record = private_method.getCurrentDetailRecord();
					uploadfiles.upload()
					
				},	
				
				//删除上传文件
				delupload:function(){
					_moduleConfig = sysmanMain.getCurrentModuConfig();
					if(_moduleConfig.editWindow)
						uploadfiles.del({
							biztableid:_moduleConfig.editWindow + ' div.table-archive',
							rootPath:_moduleConfig.bizProto.rootPath})
				},
				
				//检测audit状态
				/**
				 * @return true ,已审核;false,未审核
				 */
				checkAuditStatus:function(){
					_moduleConfig = sysmanMain.getCurrentModuConfig();
					$form = $('#'+_moduleConfig.editWindow).find('form.main-form')
					//审核状态判断
					if(_moduleConfig.auditEnable){
						var _status =$($form).find('#status').val()
						if(_status && (_status>0)){
							
							return true
						}
					}
					
					return false
				},
				//初始化元素
				init : function() {
					_moduleConfig = sysmanMain.getCurrentModuConfig();				
					var _tab = sysmanMain.getCurrentTab();
					$grid = $('#' + _moduleConfig.rootId +' ' + gridSelector);					
					
					// 编辑窗口初始化									
					$form = $('#' +_moduleConfig.editWindow + ' form.main-form')
					$form.form('clear')
					// 编辑窗口初始化					
					var editTd = $('#' +_moduleConfig.editWindow + ' ' + _moduleConfig.editbtnSelector)
					
					var _fullBizcode = sysmanMain.getCurrentBizcode();
					if(editTd && editTd.length>0){
						var _param = {
							bizcode:_fullBizcode,
							rootPath:_bizProto.rootPath,
							container:editTd,
							callback:private_method.bindBtnEvent}
						btnServ.generateEditBtn(_param);
					}
					
					//页面初始化参数
					var _initParam = {form:$form}
					//加载流程执行信息
					var selectedRecord
					if($grid.length==1){
					selectedRecord=$grid.datagrid('getSelected');
					if(selectedRecord){
						
						$form.form('load',selectedRecord)
						_historicGrid= $('#' +_moduleConfig.editWindow + ' table.historics')						
						
						if(_historicGrid && _historicGrid.length>0){
							//初始化节点信息查询配置						
							var _historicParam ={
								url : _bizProto.flowHistoricUrl,
								queryParams:{flowInstId:selectedRecord.flowInstId}	
							}
							
							//流程节点配置有外部参数
							if(_moduleConfig.historicUrl){
								_historicParam.url =_bizProto.rootPath+ _moduleConfig.historicUrl
							}
								
							if(_moduleConfig.historicField){
								var _hisQryParam = {}
								var _field = _moduleConfig.historicField
								var _fieldValue = selectedRecord[_moduleConfig.historicField]
								if(window.console){
									console.info(' fieldName:' +  _field +',fieldValue:'+_fieldValue)
								}
								_hisQryParam[_field]=_fieldValue
								_historicParam.queryParams = _hisQryParam
							}
							
							var gridOptions = _historicGrid.datagrid('options')
							if(gridOptions.url)
								_historicGrid.datagrid('load',_historicParam.queryParams);
							else
								_historicGrid.datagrid(_historicParam);
						}
						//判断是否调用组件初始化方法						
						_initParam.record=selectedRecord						
					}
					}
					
					if(_moduleConfig.rootId){
						_initParam.rootId = _moduleConfig.rootId
					}
					//判断是否执行页面初始化
					if(_moduleConfig.initElement){_moduleConfig.initElement(_initParam)}
				},
				/**
				 * 
				 */
				changePanel:function(_options){
					
					_moduleConfig =sysmanMain.getCurrentModuConfig();
					var $listPanel = $('#' +_moduleConfig.editWindow +'  div.edit-panel' )
					var $formPanel = $('#' +_moduleConfig.editWindow +' div.upload-panel' )
					
					panelUtil.togglePanel({target:$listPanel})
					panelUtil.togglePanel({target:$formPanel})
				},
				//绑定按钮事件
				bindBtnEvent:function(){
					_moduleConfig =sysmanMain.getCurrentModuConfig();
					if(_moduleConfig.editWindow){
						$edit_window = $('#'+_moduleConfig.editWindow)						
					}
					window_btn = $($edit_window).find('a.function-btn')
					$(window_btn).unbind('click')
					$(window_btn).bind('click',	private_method.function_btn_listenner)
					
					var file_btn = $($edit_window).find(' a.file_upload_btn')
					$(file_btn).unbind('click')
					$(file_btn).bind('click',	private_method.function_btn_listenner)
				},				
				
				// 功能按钮监听函数
				function_btn_listenner : function(e) {

					btnServ.applyButton({buttonEvt:e,buttonConfig:_buttonConfig})

				},
				
				// 提交form
				submit : function() {
					
					_moduleConfig = sysmanMain.getCurrentModuConfig();
					var extendsData
					if(_moduleConfig.beforeSubmit){
						extendsData =_moduleConfig.beforeSubmit()
						if(!extendsData){
							msgServ.Msgslide('请填写必填项');
							return
						}
					}
					$form = $('#'+_moduleConfig.editWindow).find('form.main-form')
					
					
					if (window.console) {
						console.info(_moduleConfig.moduleCode +' submit on air ...');
					}
					
					
					
					//审核判断					
					if(private_method.checkAuditStatus()){
						msgServ.Msgslide('已审核记录无法修改记录内容')
						return
					}
					var _tab = sysmanMain.getCurrentTab();
					$grid = $(_tab).find(gridSelector);
					var _param = {form:$form,grid:$grid,url:_moduleConfig.bizProto.saveUrl}
					
					if(_moduleConfig.fullBizcode){						
						_param.fullBizcode = _moduleConfig.fullBizcode
					}
					if(_moduleConfig.saveValidate){
						_param.validate=_moduleConfig.saveValidate
					}
					if(extendsData){
						_param.externalValue=extendsData
					}
					if(_moduleConfig.afterSubmit){
						_param.callback = _moduleConfig.afterSubmit
					}
					datagridServ.save(_param)					
				},
				//
				cancel : function() {
					$($edit_window).window('close');
				},
				/**
				 * 获取当前明细列表中选中记录
				 */
				getCurrentRecord:function(){
					var _record
					var _tab = sysmanMain.getCurrentTab();
					var _grid = $(_tab).find(gridSelector);
					return $(_grid).datagrid('getSelected');
				},
				getCurrentStatus:function(){
					_moduleConfig = sysmanMain.getCurrentModuConfig();
					$form = $('#'+_moduleConfig.editWindow).find('form.main-form')
					var _id = $($form).find('#id').val()
					var _status = $($form).find('#status').val()
					if(!_id  || $.trim(_id)==''){
						return null;
					}
					return {id:_id,status:_status}
				},
				/**
				 * 获取审核参数
				 */
				getAuditParam:function(){
					if(window.console){
						console.log('get audit param loaded ')	
					}
					_moduleConfig = sysmanMain.getCurrentModuConfig();
					var tab = sysmanMain.getCurrentTab();					
					$grid = $(tab).find(gridSelector);
					var _param = {						
						moduleConfig:_moduleConfig,
						grid:$grid
					}
					if(_moduleConfig.editWindow){
						_param.editWindow = _moduleConfig.editWindow
					}else{
						_param.editWindow = _moduleConfig.edit_window
					}
					return _param
				},
				
				/**
				 * 提交审核
				 */
				auditSubmit:function(){
					
					buttonEvent.auditSubmit(private_method.getAuditParam())
				},
				/**
				 * 审核通过
				 */
				auditPermit:function(){
					
					buttonEvent.auditPermit(private_method.getAuditParam())
				},
				/**
				 * 审核未通过
				 */
				auditDenied:function(){
					
					buttonEvent.auditDenied(private_method.getAuditParam())
				}
				
				

			};
			
			return public_method;

		});